﻿using SageKing.Core.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SageKing.Core.Extensions;

public static class MessageModelFormExts
{

    public static void LoadDataFrom<Tatt>(this MessageModelForm model, List<Tatt> getAttData)
        where Tatt : SysSageKingMessageAttributeBase
    {
        var groupData = getAttData.GroupBy(a => a.Type).ToList();
        foreach (var attributes in groupData)
        {
            var item = attributes.ToList();
            switch (attributes.Key)
            {
                case DataStreamTypeEnum.String:
                    model.AttStrLst ??= new List<KeyValueClass<string, string>>();
                    model.AttStrLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Int8:
                    model.AttsbyteLst ??= new List<KeyValueClass<string, sbyte>>();
                    model.AttsbyteLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Uint8:
                    model.AttbyteLst ??= new List<KeyValueClass<string, byte>>();
                    model.AttbyteLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Int16:
                    model.AttshortLst ??= new List<KeyValueClass<string, short>>();
                    model.AttshortLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Uint16:
                    model.AttushortLst ??= new List<KeyValueClass<string, ushort>>();
                    model.AttushortLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Int32:
                    model.AttintLst ??= new List<KeyValueClass<string, int>>();
                    model.AttintLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Uint32:
                    model.AttuintLst ??= new List<KeyValueClass<string, uint>>();
                    model.AttuintLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Int64:
                    model.AttlongLst ??= new List<KeyValueClass<string, long>>();
                    model.AttlongLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Uint64:
                    model.AttulongLst ??= new List<KeyValueClass<string, ulong>>();
                    model.AttulongLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Float32:
                    model.AttfloatLst ??= new List<KeyValueClass<string, float>>();
                    model.AttfloatLst.AddOrInitLst(item);
                    break;
                case DataStreamTypeEnum.Float64:
                    model.AttdoubleLst ??= new List<KeyValueClass<string, double>>();
                    model.AttdoubleLst.AddOrInitLst(item);
                    break;
                default:
                    break;
            }
        }
    }

    public static void AddOrInitLst<T,Tatt>(this List<KeyValueClass<string, T>> attLst, Tatt att)
        where Tatt: SysSageKingMessageAttributeBase
    {
        attLst.Add(new KeyValueClass<string, T>()
        {
            Key = att.Name,
            ColumnConfig =
            new ColumnOuput()
            {
                IsNullable = !att.Required,
                ColumnComment = att.Description,
                ColumnName = att.DisplayName ?? att.Name
            }
        });
    }

    public static void AddOrInitLst<T, Tatt>(this List<KeyValueClass<string, T>> attLst, List<Tatt> atts)
        where Tatt : SysSageKingMessageAttributeBase
    {
        var list = new List<KeyValueClass<string, T>>();
        foreach (var att in atts)
        {
            list.AddOrInitLst(att);
        }
        attLst.AddRange(list);
    }


}
